package com.sc.content;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sc.utils.ADLog;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class CollectionDataDao {
	private ContentResolver contentResolver;
	public void openDB(Context context) {
		try {
			contentResolver = context.getContentResolver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(CollectionDataDO collectionDataDO) {
		try {
			ContentValues values = new ContentValues();
			values.put(SCTData.CollectDataDO.CID, collectionDataDO.getCid());
			values.put(SCTData.CollectDataDO.CNODE, collectionDataDO.getDnode());
			values.put(SCTData.CollectDataDO.CSTATUS, collectionDataDO.getDstatus());
			values.put(SCTData.CollectDataDO.CVOLTAGE, collectionDataDO.getDvoltage());
			values.put(SCTData.CollectDataDO.CHUMIDITY, collectionDataDO.getDhumidity());
			values.put(SCTData.CollectDataDO.CBRIGHTNESS, collectionDataDO.getDbrightness());
			values.put(SCTData.CollectDataDO.CTEMPERATURE, collectionDataDO.getDtemperature());
			values.put(SCTData.CollectDataDO.CINTERAL, collectionDataDO.getDinteral());
			values.put(SCTData.CollectDataDO.CSAVE, collectionDataDO.getDsave());
			values.put(SCTData.CollectDataDO.CSMESSAGE, collectionDataDO.getDsmessage());
			values.put(SCTData.CollectDataDO.CSINTIME, collectionDataDO.getDsintime());
			values.put(SCTData.CollectDataDO.CINTIME, collectionDataDO.getDintime());
			contentResolver.insert(SCTData.CollectDataDO.WORD_CONTENT_URI, values);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteById(Long cId) {
		try {
			String selection = "cid=?";
			String[] selectionArgs = new String[]{""+cId};
			contentResolver.delete(SCTData.CollectDataDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteByInTime(Long intime) {
		try {
			String selection = "cintime<?";
			String[] selectionArgs = new String[]{""+intime};
			contentResolver.delete(SCTData.CollectDataDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteByInTimeByStatus(Long intime) {
		try {
			String selection = "cintime<? and cstatus=1";
			String[] selectionArgs = new String[]{""+intime};
			contentResolver.delete(SCTData.CollectDataDO.DICT_CONTENT_URI, selection, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void updateCurrentDate(Long now) {
		try {
			ContentValues values = new ContentValues();
			values.put(SCTData.CollectDataDO.CSTATUS, 1);
			String where = "cintime<?";
			String[] selectionArgs = new String[]{""+now};
			contentResolver.update(SCTData.CollectDataDO.DICT_CONTENT_URI, values, where, selectionArgs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public CollectionDataDO getById(Long cid) {
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
												SCTData.CollectDataDO.CNODE,
												SCTData.CollectDataDO.CSTATUS,
												SCTData.CollectDataDO.CVOLTAGE,
												SCTData.CollectDataDO.CHUMIDITY,
												SCTData.CollectDataDO.CBRIGHTNESS,
												SCTData.CollectDataDO.CTEMPERATURE,
												SCTData.CollectDataDO.CINTERAL,
												SCTData.CollectDataDO.CSAVE,
												SCTData.CollectDataDO.CSMESSAGE,
												SCTData.CollectDataDO.CSINTIME,
												SCTData.CollectDataDO.CINTIME};
			String selection = "cid=?";
			String[] selectionArgs = new String[]{""+cid};
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.WORD_CONTENT_URI, projection, selection, selectionArgs, null);
			if(cursor == null || cursor.getCount() == 0)
				return null;
			cursor.moveToNext();
		
			CollectionDataDO collectionDataDO = new CollectionDataDO();
			collectionDataDO.setCid(cursor.getLong(0));
			collectionDataDO.setDnode(cursor.getInt(1));
			collectionDataDO.setDstatus(cursor.getInt(2));
			collectionDataDO.setDvoltage(cursor.getInt(3));
			collectionDataDO.setDhumidity(cursor.getInt(4));
			collectionDataDO.setDbrightness(cursor.getInt(5));
			collectionDataDO.setDtemperature(cursor.getInt(6));
			collectionDataDO.setDinteral(cursor.getInt(7));
			collectionDataDO.setDsave(cursor.getInt(8));
			collectionDataDO.setDsmessage(cursor.getString(9));
			collectionDataDO.setDsintime(cursor.getString(10));
			collectionDataDO.setDintime(cursor.getLong(11));
			return collectionDataDO;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<CollectionDataDO> findAll(String node) {
		List<CollectionDataDO> res = new ArrayList<CollectionDataDO>();
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime asc";
			String selection = "cnode=?";
			String[] selectionArgs = new String[]{node};
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				res.add(collectionDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	public List<CollectionDataDO> findAllData() {
		List<CollectionDataDO> res = new ArrayList<CollectionDataDO>();
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime asc";
			String selection = "";
			String[] selectionArgs = new String[]{};
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				res.add(collectionDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	public List<CollectionDataDO> findCurrentData(long now) {
		List<CollectionDataDO> res = new ArrayList<CollectionDataDO>();
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime asc";
			String selection = "cintime<? and cstatus=-1";
			String[] selectionArgs = new String[]{""+now};
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				res.add(collectionDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}	
	public List<CollectionDataDO> findPage(int pagesize, int page, String node) {
		List<CollectionDataDO> res = new ArrayList<CollectionDataDO>();
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime desc";
			String limit = ""+pagesize+" offset " + (page*pagesize);
			String selection = "cnode=?";
			String[] selectionArgs = new String[]{node};
			//String limit = "1,0";
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder+"|"+limit);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				res.add(collectionDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public List<CollectionDataDO> findPageCurrent(int pagesize, int page, String node) {
		List<CollectionDataDO> res = new ArrayList<CollectionDataDO>();
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime desc";
			String limit = ""+pagesize+" offset " + (page*pagesize);
			long current = new Date().getTime() - 1000*60;
			String selection = "cnode=? and cintime>=?";
			String[] selectionArgs = new String[]{node, ""+current};
			//String limit = "1,0";
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder+"|"+limit);
			if(cursor == null || cursor.getCount() == 0)
				return res;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				res.add(collectionDataDO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public CollectionDataDO findCurrent(String node) {
		try {
			String[] projection = new String[]{SCTData.CollectDataDO.CID,
					SCTData.CollectDataDO.CNODE,
					SCTData.CollectDataDO.CSTATUS,
					SCTData.CollectDataDO.CVOLTAGE,
					SCTData.CollectDataDO.CHUMIDITY,
					SCTData.CollectDataDO.CBRIGHTNESS,
					SCTData.CollectDataDO.CTEMPERATURE,
					SCTData.CollectDataDO.CINTERAL,
					SCTData.CollectDataDO.CSAVE,
					SCTData.CollectDataDO.CSMESSAGE,
					SCTData.CollectDataDO.CSINTIME,
					SCTData.CollectDataDO.CINTIME};
			String sortOrder = "cintime desc";
			String limit = "1 offset 0";
			long current = new Date().getTime() - 1000*10;
			//ADLog.debug("==========="+current);
			String selection = "cnode=? and cintime>=?";
			String[] selectionArgs = new String[]{node, ""+current};
			//String limit = "1,0";
			Cursor cursor = contentResolver.query(SCTData.CollectDataDO.DICT_CONTENT_URI, projection, selection, selectionArgs, sortOrder+"|"+limit);
			if(cursor == null || cursor.getCount() == 0)
				return null;
			while (cursor.moveToNext()) {
				CollectionDataDO collectionDataDO = new CollectionDataDO();
				collectionDataDO.setCid(cursor.getLong(0));
				collectionDataDO.setDnode(cursor.getInt(1));
				collectionDataDO.setDstatus(cursor.getInt(2));
				collectionDataDO.setDvoltage(cursor.getInt(3));
				collectionDataDO.setDhumidity(cursor.getInt(4));
				collectionDataDO.setDbrightness(cursor.getInt(5));
				collectionDataDO.setDtemperature(cursor.getInt(6));
				collectionDataDO.setDinteral(cursor.getInt(7));
				collectionDataDO.setDsave(cursor.getInt(8));
				collectionDataDO.setDsmessage(cursor.getString(9));
				collectionDataDO.setDsintime(cursor.getString(10));
				collectionDataDO.setDintime(cursor.getLong(11));
				return collectionDataDO;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
